package com.ruoyi.web.controller.fs;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.fs.domain.FsCase;
import com.ruoyi.fs.domain.FsWorkOrderPost;
import com.ruoyi.fs.service.IFsCaseService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.fs.domain.FsWorkOrderPush;
import com.ruoyi.fs.service.IFsWorkOrderPushService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 重提工单Controller
 *
 * @author hero
 * @date 2024-10-28
 */
@RestController
@RequestMapping("/fs/push")
public class FsWorkOrderPushController extends BaseController {
    @Autowired
    private IFsWorkOrderPushService fsWorkOrderPushService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private IFsCaseService fsCaseService;

    /**
     * 查询重提工单列表
     */
    @PreAuthorize("@ss.hasPermi('fs:push:list')")
    @GetMapping("/list")
    public TableDataInfo list(FsWorkOrderPush fsWorkOrderPush) {
        startPage();
        List<FsWorkOrderPush> list = fsWorkOrderPushService.selectFsWorkOrderPushList(fsWorkOrderPush);
        for (int i = 0; i < list.size(); i++) {
            Long cjr = list.get(i).getCjr();
            if (cjr != null) {
                SysUser user = sysUserService.selectUserById(cjr);
                list.get(i).setCjrMc(user.getNickName());
            }
            Long ssr = list.get(i).getSsr();
            if (ssr != null) {
                SysUser user = sysUserService.selectUserById(ssr);
                list.get(i).setSsrMc(user.getNickName());
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出重提工单列表
     */
    @PreAuthorize("@ss.hasPermi('fs:push:export')")
    @Log(title = "重提工单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FsWorkOrderPush fsWorkOrderPush) {
        List<FsWorkOrderPush> list = fsWorkOrderPushService.selectFsWorkOrderPushList(fsWorkOrderPush);
        ExcelUtil<FsWorkOrderPush> util = new ExcelUtil<FsWorkOrderPush>(FsWorkOrderPush.class);
        util.exportExcel(response, list, "重提工单数据");
    }

    /**
     * 获取重提工单详细信息
     */
    @PreAuthorize("@ss.hasPermi('fs:push:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(fsWorkOrderPushService.selectFsWorkOrderPushById(id));
    }

    /**
     * 新增重提工单
     */
    @PreAuthorize("@ss.hasPermi('fs:push:add')")
    @Log(title = "重提工单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FsWorkOrderPush fsWorkOrderPush) {
        return toAjax(fsWorkOrderPushService.insertFsWorkOrderPush(fsWorkOrderPush));
    }

    /**
     * 修改重提工单
     */
    @PreAuthorize("@ss.hasPermi('fs:push:edit')")
    @Log(title = "重提工单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FsWorkOrderPush fsWorkOrderPush) {
        return toAjax(fsWorkOrderPushService.updateFsWorkOrderPush(fsWorkOrderPush));
    }

    /**
     * 删除重提工单
     */
    @PreAuthorize("@ss.hasPermi('fs:push:remove')")
    @Log(title = "重提工单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(fsWorkOrderPushService.deleteFsWorkOrderPushByIds(ids));
    }

    /**
     * 开始工单
     */
    @PreAuthorize("@ss.hasPermi('fs:push:query')")
    @GetMapping(value = "/start/{id}")
    public AjaxResult start(@PathVariable("id") Long id) {
        FsWorkOrderPush push = fsWorkOrderPushService.selectFsWorkOrderPushById(id);
        push.setZt(2);
        return success(fsWorkOrderPushService.updateFsWorkOrderPush(push));
    }

    /**
     * 开始工单
     */
    @PreAuthorize("@ss.hasPermi('fs:push:query')")
    @GetMapping(value = "/complete/{id}")
    public AjaxResult complete(@PathVariable("id") Long id) {
        FsWorkOrderPush push = fsWorkOrderPushService.selectFsWorkOrderPushById(id);
        LoginUser loginUser = getLoginUser();
        SysUser user = loginUser.getUser();
        if (user != null) {
            push.setSsr(user.getUserId());
        }
        push.setZt(3);
        push.setWcsj(new Date());
        fsWorkOrderPushService.updateFsWorkOrderPush(push);
        FsCase fsCase = fsCaseService.selectFsCaseById(push.getCaseId());
        fsCase.setIfRepush(1);
        int pushNum = fsCase.getRepushNum();
        pushNum++;
        fsCase.setRepushNum(pushNum);
        fsCaseService.updateFsCase(fsCase);
        return success();
    }
}
